package com.org.daniel.framework.shiro.realm;

import com.org.daniel.module.entity.manager.ManagerPermission;
import com.org.daniel.module.entity.manager.ManagerRole;
import com.org.daniel.module.entity.manager.ManagerRolePermissionRelation;
import com.org.daniel.module.entity.manager.ManagerUser;
import com.org.daniel.service.manager.ManagerPermissionService;
import com.org.daniel.service.manager.ManagerRolePermissionRelationService;
import com.org.daniel.service.manager.ManagerRoleService;
import com.org.daniel.service.manager.ManagerUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 自定义Realm 处理登录 权限
 * @author Daniel
 */
public class EnceladusShiroRealm extends AuthorizingRealm {

	@Autowired
	private ManagerUserService managerUserService;
	@Autowired
	private ManagerRoleService managerRoleService;
	@Autowired
	private ManagerPermissionService managerPermissionService;
	@Autowired
	private ManagerRolePermissionRelationService managerRolePermissionRelationService;

	/**
	 * 授权
	 * @param principals PrincipalCollection
	 * @return AuthorizationInfo
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		String username = (String) principals.getPrimaryPrincipal();
		ManagerUser managerUser = managerUserService.findByUsername(username);
		String roleSeq = managerUser.getRoleSeq();
		if (StringUtils.isBlank(roleSeq)){
			return authorizationInfo;
		}
		ManagerRole managerRole = managerRoleService.findBySeq(roleSeq);
		if (managerRole != null){
			authorizationInfo.addRole(managerRole.getRoleName());
		}
		List<ManagerRolePermissionRelation> managerRolePermissionRelations
				= managerRolePermissionRelationService.findByRoleSeq(roleSeq);
		managerRolePermissionRelations.forEach(managerRolePermissionRelation -> {
			String permissionSeq = managerRolePermissionRelation.getPermissionSeq();
			ManagerPermission managerPermission = managerPermissionService.findBySeq(permissionSeq);
			authorizationInfo.addStringPermission(managerPermission.getPermissionName());
		});
		return authorizationInfo;
	}

	/**
	 * 登录认证
	 * @param token AuthenticationToken
	 * @return AuthenticationInfo
	 * @throws AuthenticationException 异常
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = (String) token.getPrincipal();
		ManagerUser managerUser = managerUserService.findByUsername(username);
		if (managerUser == null){
			return null;
		}
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
				managerUser.getUsername(), managerUser.getPassword(),
				ByteSource.Util.bytes(managerUser.getSalt()), getName());
		return authenticationInfo;
	}
}
